<template>
  <div class="chat-container">
    <div class="chat-header">
      <h1>DeepSeek 聊天助手</h1>
    </div>

    <div class="chat-messages" ref="messagesContainer">
      <div
          v-for="(msg, index) in messages"
          :key="index"
          :class="['message', msg.role]"
      >
        <div class="avatar">
          {{ msg.role === 'user' ? '我' : 'AI' }}
        </div>
        <div class="content">{{ msg.content }}</div>
      </div>
      <div v-if="loading" class="loading">正在思考中...</div>
    </div>

    <div class="chat-input">
      <textarea
          v-model="inputMessage"
          placeholder="请输入消息..."
          @keydown.enter.exact="sendMessage"
          @keydown.enter.shift="() => {}"
      ></textarea>
      <button @click="sendMessage" :disabled="!inputMessage.trim() || loading">
        发送
      </button>
    </div>
  </div>
</template>

<script>
import deepSeekApi from '../api/deepSeek_api.js'

export default {
  data() {
    return {
      messages: [],
      inputMessage: '',
      loading: false
    }
  },
  methods: {
    async sendMessage() {
      const message = this.inputMessage.trim()
      if (!message || this.loading) return

      // 添加用户消息到聊天记录
      this.messages.push({ role: 'user', content: message })
      this.inputMessage = ''
      this.loading = true

      try {
        // 调用API获取数据
        const response = await deepSeekApi.chat(message)

        // 处理响应数据 - 重点修改：解析Result封装的结构
        const responseData = response;

        // 提取回复内容（注意这里多了一层data，因为Result结构包含data字段）
        const content = responseData?.choices?.[0]?.message?.content
            || '未获取到有效回复'

        this.messages.push({ role: 'assistant', content: content })
      } catch (error) {
        this.messages.push({
          role: 'assistant',
          content: `发生错误: ${error.message || '未知错误'}`
        })
        console.error('请求失败:', error)
      } finally {
        this.loading = false
        this.scrollToBottom()
      }
    },

    scrollToBottom() {
      this.$nextTick(() => {
        const container = this.$refs.messagesContainer
        container.scrollTop = container.scrollHeight
      })
    }
  },
  mounted() {
    this.messages.push({
      role: 'assistant',
      content: '你好！我是DeepSeek AI助手，有什么可以帮助你的吗？'
    })
  }
}
</script>

<style scoped>
.chat-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 800px;
  margin: 0 auto;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  overflow: hidden;
}

.chat-header {
  background-color: #f5f5f5;
  padding: 16px;
  text-align: center;
  border-bottom: 1px solid #e0e0e0;
}

.chat-messages {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
  background-color: #fafafa;
}

.message {
  display: flex;
  margin-bottom: 16px;
  max-width: 80%;
}

.message.user {
  flex-direction: row-reverse;
  margin-left: auto;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background-color: #eee;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 8px;
  font-size: 14px;
}

.message.user .avatar {
  margin-right: 0;
  margin-left: 8px;
  background-color: #007bff;
  color: white;
}

.content {
  padding: 8px 16px;
  border-radius: 18px;
  background-color: #e9e9eb;
  line-height: 1.5;
}

.message.user .content {
  background-color: #007bff;
  color: white;
}

.loading {
  text-align: center;
  padding: 16px;
  color: #666;
}

.chat-input {
  display: flex;
  padding: 16px;
  border-top: 1px solid #e0e0e0;
}

textarea {
  flex: 1;
  padding: 12px;
  border: 1px solid #e0e0e0;
  border-radius: 24px;
  resize: none;
  outline: none;
  font-size: 14px;
  min-height: 48px;
}

button {
  margin-left: 12px;
  padding: 0 24px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 24px;
  cursor: pointer;
  white-space: nowrap;
}

button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}
</style>